package com.pampasoft.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.opentok.api.OpenTokSDK;
import com.opentok.api.OpenTokSession;
import com.opentok.api.constants.SessionProperties;
import com.opentok.exception.OpenTokException;
import com.pampasoft.form.PampaForm;
import com.pampasoft.service.PampaService;
import com.pampasoft.service.impl.PampaServiceImpl;


@Controller
public class SessionController {

        private static final PampaService pampaSvc = new PampaServiceImpl();
        private String serverIP;
        
        @RequestMapping(value={"/","/index","home"})
        public ModelAndView initialValues (HttpServletRequest request, HttpServletResponse response){
                serverIP = request.getLocalAddr();
                request.setAttribute("api_key", PampaService.API_KEY);
                request.setAttribute("api_secret", PampaService.API_SECRET);
                request.setAttribute("pampaForm", new PampaForm());
                
                return new ModelAndView("/index");
        }
        
        @RequestMapping(value="/create-session", method=RequestMethod.POST)
        public @ResponseBody PampaForm createSession(HttpServletRequest request, HttpServletResponse response){
                //Start the SDK
                OpenTokSDK sdk = new OpenTokSDK(PampaService.API_KEY, PampaService.API_SECRET);

                //Generate Session Properties for a session
                SessionProperties sp = new SessionProperties();
                sp.echoSuppression_enabled = true;
                OpenTokSession session = null;
                String token = null;
                try {
                        //Generate a session extracting the IP from the Request
                        session = sdk.create_session(serverIP);
                        //Generate a token
                        token = sdk.generate_token(session.session_id);
                        //Make a service to store the online sessions
                        //pampaSvc.storeOnlineSession(session);
                } catch (OpenTokException e) {
                        // TODO: handle exception
                        e.printStackTrace();
                }
                PampaForm form = new PampaForm();
                form.setSession_id(session.getSessionId());
                form.setSession_token(token);
                request.setAttribute("session_id", session.session_id);
                request.setAttribute("pass_token", token);
                request.setAttribute("pampaForm", form);
                return form;
        }
        @RequestMapping(value="/create-token", method=RequestMethod.POST)
        public @ResponseBody PampaForm createToken(@RequestBody String sessionId, HttpServletResponse response){
                //Start the SDK
                OpenTokSDK sdk = new OpenTokSDK(PampaService.API_KEY, PampaService.API_SECRET);

                //Generate Session Properties for a session
                SessionProperties sp = new SessionProperties();
                sp.echoSuppression_enabled = true;
                String token = null;
                try {
                        //Generate a token
                        token = sdk.generate_token(sessionId);
                        //Make a service to store the online sessions
                        //pampaSvc.storeOnlineSession(session);
                } catch (OpenTokException e) {
                        // TODO: handle exception
                        e.printStackTrace();
                }
                PampaForm form = new PampaForm();
                form.setSession_id(sessionId);
                form.setSession_token(token);
                return form;
        }
        
        @RequestMapping(value="/get-api-key", method=RequestMethod.POST)
        public @ResponseBody String getApiKey (HttpServletRequest request, HttpServletResponse response){
                return Integer.toString(PampaService.API_KEY);
        }
        @RequestMapping(value="/get-api-secret", method=RequestMethod.POST)
        public @ResponseBody String getApiSecret (HttpServletRequest request, HttpServletResponse response){
                return PampaService.API_SECRET;
        }
        
}